package xj.util.gui;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class ExtensionFileFilter extends FileFilter {

  public ExtensionFileFilter(String ext, String des) {
    this(ext, des, -1);
  }

  public ExtensionFileFilter(String ext, String des, int ftype) {
    extension = ext;
    description = des;
    filetype = ftype;
  }

  @Override
  public boolean accept(File f) {
    if (f != null) {
      if (f.isDirectory()) {
	return true;
      }
      String ext = getFileExtension(f);
      if (ext != null && ext.equals(extension)) {
	return true;
      }
      ;
    }
    return false;
  }

  @Override
  public String getDescription() {
    return description;
  }

  public String getExtension() {
    return extension;
  }

  public int getFileType() {
    return filetype;
  }

  static public String getFileExtension(File f) {
    if (f != null) {
      String filename = f.getName();
      int i = filename.lastIndexOf('.');
      if (i > 0 && i < filename.length() - 1) {
	String fext = filename.substring(i + 1);
	if (fext != null) {
	  return fext.toLowerCase();
	}
      }
    }
    return null;
  }

  protected String extension;

  protected String description;

  protected int filetype;

}
